#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: Beyondzy
@file: 美团评论.py
@time: 2022/03/01
@desc:
"""
import csv
import pprint

import requests


def start():
    f = open('comments.csv','a',encoding='utf-8',newline='')
    csv_writer = csv.DictWriter(f,fieldnames=[
        '用户名',
        '评论',
        '评分',
        '菜单',
        '商家回评'
    ])
    csv_writer.writeheader()
    #表体
    for w in range(0,1600,10):
        url = 'https://bj.meituan.com/meishi/api/poi/getMerchantComment?uuid=7116d8b2b9a0487188c7.1646119245.1.0.0&platform=1&partner=126&originUrl=https%3A%2F%2Fbj.meituan.com%2Fmeishi%2F350451%2F&riskLevel=1&optimusCode=10&id=350451&userId=2946395421&offset='+str(w)+'&pageSize=10&sortType=1'
        headers={"Accept": "application/json", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6", "Connection": "keep-alive", "Cookie": "_lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_cuid=17f42e0b524c8-07f56a515080c1-a3e3164-1fa400-17f42e0b524c8; mtcdn=K; client-id=933ad90c-7e54-48ea-b18e-6f4bbaf9fada; _hc.v=a193fdd0-b0fd-9fef-29e7-e8b21376df71.1646094586; ci=1; rvct=1%2C70; uuid=7116d8b2b9a0487188c7.1646119245.1.0.0; WEBDFPID=5y2wy5639022583yz20w14x0ux707vw582u554z4zx297958uu0v0zu9-1646205647347; lt=ZWF4l4-y7Kj9Mi6A0rj_eGFs4k0AAAAAjBAAAJICCn-ihR49H5-a4QypM_L0JQpWPd-eUUtPlA2Y0XqtOz8RVZAnDjOCi1l36X4x5g; u=2946395421; n=vxl805079915; token2=ZWF4l4-y7Kj9Mi6A0rj_eGFs4k0AAAAAjBAAAJICCn-ihR49H5-a4QypM_L0JQpWPd-eUUtPlA2Y0XqtOz8RVZAnDjOCi1l36X4x5g; unc=vxl805079915; __mta=88839588.1646094504768.1646119816619.1646122829643.6; _lxsdk=17f42e0b524c8-07f56a515080c1-a3e3164-1fa400-17f42e0b524c8; lat=39.983375; lng=116.410765; firstTime=1646122850697; _lxsdk_s=17f445a5973-f76-bc6-0b1%7C%7C151", "Host": "bj.meituan.com", "Referer": "https://bj.meituan.com/meishi/350451/", "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"98\", \"Google Chrome\";v=\"98\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-origin", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"}
        result = requests.get(url=url,headers=headers)
        res = result.json()['data']['comments']
        print(len(res))
        for index in res:
            pprint.pprint(index)
            dict={
                '用户名':index['userName'],
                '评论':index['comment'],
                '评分':index['star'],
                '菜单':index['menu'],
                '商家回评':index['merchantComment']
            }
            print(dict)
            print("++++++++++++++++")
            csv_writer.writerow(dict)
            print(dict)

if __name__ == '__main__':
    start()
